﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Landscape;
using Landscape.Members;

namespace Landscape.Web.Controls {
    public class InlineXSLTControl : System.Web.UI.WebControls.WebControl {
        //this is supposed to get rid of the span tag
        protected override void Render(HtmlTextWriter writer) {
            this.RenderContents(writer);
        }  
        protected Literal litResult;
        public string Expression {
            get;
            set;
        }
        public object DataSource {
            get;
            set;
        }
        public string DataSourceXml {
            set {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(value);
                this.DataSource = doc;
            }
        }
        public bool DisableOutputEscaping {
            get;
            set;
        }
        protected override void OnInit(EventArgs e) {
            this.litResult = new Literal();
            this.Controls.Add(litResult);
            base.OnInit(e);
        }
        protected override void OnLoad(EventArgs e) {
            if (!string.IsNullOrEmpty(this.Expression)) {
                if (this.DataSource == null) {
                    var doc = new System.Xml.XmlDocument();
                    doc.LoadXml("<root/>");
                    this.DataSource = doc;
                }

                InlineExpression expr = new InlineExpression() {
                    DataSource = DataSource,
                    OutputMethod = Landscape.Context.xslOutputMethod,
                    DisableOutputEscaping = DisableOutputEscaping,
                    XPath = Expression
                };

                litResult.Text =expr.Compile();
            }
            base.OnLoad(e);
        }
    }
}